define(function(require, exports, module) {

    var Validator = require('bootstrap.validator');
        require('common/validator-rules').inject(Validator);
        require('jquery.form');

    exports.run = function() {

        // require('./header').run();

        var validator = new Validator({
            element: '#question-import-form',
            autoSubmit: false,
            onFormValidated: function(error, results, $form) {
                if (error) {
                    return false;
                }

                $('#question-import-btn').addClass('disabled').button('reading');

                $form.ajaxSubmit({
                    success: function(html){

                        if ($(html).data('error') == true) {

                            $('#question-import-btn').removeClass('disabled').button('reset');
                            $('#questions-show-board').replaceWith($(html));

                        } else {

                            $('#import-file').clearFields();
                            $('#question-import-form').hide();
                            $('#questions-show-board').html(html);

                        }
                    },
                    error: function(response){
                        var error = JSON.parse(response);
                        $('#question-import-btn').button('error');
                        $('#questions-show-board').addClass('well').text(error).show();
                    }
                });

            }
        });

        validator.addItem({
            element: '#import-file',
            required: true,
            display: '文本文件',
        });

        validator.addItem({
            element: '#import-target',
            required: true
        });

    };

});